package com.fuhao.product;

import com.fuhao.core.base.BaseApiService;
import com.fuhao.core.base.BaseResponse;
import com.fuhao.dto.output.ProductDto;
import com.fuhao.elk.annotation.SysLog;
import com.fuhao.elk.constants.KafkaTopicConstants;
import com.fuhao.es.entity.ProductEntity;
import com.fuhao.es.repositoty.ProductResipototy;
import com.fuhao.goods.service.ProductSearchService;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhang
 * @title: SPU搜索
 */
@RestController
public class ProductSearchServiceImpl extends BaseApiService<List<ProductDto>> implements ProductSearchService {

    @Autowired
    private ProductResipototy productResipototy;

    @SysLog(topic = KafkaTopicConstants.GOODS_TOPIC,value = "搜索商品")
    public BaseResponse<List<ProductDto>> search(String name) {
        //int i = 1/0;
        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        // 拼接查询条件
        //builder.must(QueryBuilders.fuzzyQuery("name", name));
        //模糊匹配多个字段
        builder.must(QueryBuilders.multiMatchQuery(name,"name","subtitle","detail"));
        //分页
        Pageable pageable = new QPageRequest(0, 5);
        //查询
        Page<ProductEntity> page = productResipototy.search(builder, pageable);
        List<ProductEntity> content = page.getContent();
        //转换称dto
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        List<ProductDto> mapAsList = mapperFactory.getMapperFacade().mapAsList(content, ProductDto.class);
        return setResultSuccess(mapAsList);

    }
}
